package org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.navigation;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.opentcs.kcvehicle.communication.kc.common.byteutils.ByteUtils;

public class Point {
  //序列号,4个字节,Integer,用于定位点在整个任务中的位置，从 0 开始偶数递增（如 0、2、4、6......），以区分同一个点 ID 在任务中是否多次出现
  private byte[] serialNum;
  //路径点 ID,4个字节,Integer
  private byte[] pointId;
  //指定角度时路径点的车头角度,4个字节,float ,当 isSpecifyAngle==1 的时候这个字段才生效
  private byte[] angle;
  //是否指定路径点角度,1个字节,byte,1 表示指定，0 表示不指定
  private byte isSpecifyAngle;
  //任务中点上动作信息个数,1个字节,byte
  private byte pointActionSize;
  //保留,6个字节
  private byte[] pointRetain;
  //任务中点上动作结构体,这里数组的长度是 pointActionSize 长度
  private Action[] actions;

  public Point(
      Integer serialNum, Integer pointId, float angle, byte isSpecifyAngle, byte pointActionSize,
      Action[] actions
  ) {
    this.serialNum = ByteUtils.intToBytes(serialNum, 1);
    this.pointId = ByteUtils.intToBytes(pointId, 1);
    this.angle = ByteUtils.floatToBytes(angle);
    this.isSpecifyAngle = isSpecifyAngle;
    this.pointActionSize = pointActionSize;
    this.pointRetain = new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
        (byte) 0x00};
    this.actions = actions;
  }

  public byte[] toBytes() {
    ByteBuf byteBuf = Unpooled.buffer();
    byteBuf.writeBytes(serialNum);
    byteBuf.writeBytes(pointId);
    byteBuf.writeBytes(angle);
    byteBuf.writeByte(isSpecifyAngle);
    byteBuf.writeByte(pointActionSize);
    byteBuf.writeBytes(pointRetain);
    if (actions != null) {
      for (Action action : actions) {
        byteBuf.writeBytes(action.toBytes());
      }
    }
    int i = byteBuf.writerIndex();
    byte[] bytes = new byte[i];
    byteBuf.readBytes(bytes);
    return bytes;
  }
}
